/*
 * Copyright (c) 2023, LXMS (lxinet.cn).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.lxinet.lxms.system.controller;

import cn.lxinet.lxms.framework.core.controller.BaseController;
import cn.lxinet.lxms.framework.core.vo.Result;
import cn.lxinet.lxms.system.convert.SysDeptConvert;
import cn.lxinet.lxms.system.dto.sysDept.SysDeptCreateDto;
import cn.lxinet.lxms.system.dto.sysDept.SysDeptPageDto;
import cn.lxinet.lxms.system.dto.sysDept.SysDeptUpdateDto;
import cn.lxinet.lxms.system.entity.SysDept;
import cn.lxinet.lxms.system.service.SysDeptService;
import cn.lxinet.lxms.system.vo.SysDeptVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门
 *
 * @author zcx
 * @date 2023/07/08
 */
@Slf4j
@Tag(name = "部门接口")
@RestController
@RequestMapping("/system/dept")
public class SysDeptController extends BaseController {
    @Autowired
    private SysDeptService sysDeptService;

    @GetMapping("list")
    @Operation(summary = "查询全部部门")
    @PreAuthorize("@lxp.hasPermission('sys:dept:list')")
    public Result<List<SysDeptVo>> list(@Valid SysDeptPageDto dto){
        List<SysDeptVo> list = sysDeptService.list(dto.getName(), dto.getParentId(), dto.getStatus());
        return Result.success(list);
    }

    @GetMapping("simpleList")
    @Operation(summary = "选择框部门列表")
    public Result<List<SysDeptVo>> simpleList(){
        List list = sysDeptService.simpleList();
        return Result.success(list);
    }

    @PostMapping("add")
    @Operation(summary = "新增部门")
    @PreAuthorize("@lxp.hasPermission('sys:dept:add')")
    public Result add(@Valid @RequestBody SysDeptCreateDto dto){
        sysDeptService.add(SysDeptConvert.INSTANCE.toEntity(dto));
        return Result.success();
    }

    @GetMapping("detail/{id}")
    @Operation(summary = "通过ID查询部门信息")
    @PreAuthorize("@lxp.hasPermission('sys:dept:detail')")
    public Result<SysDeptVo> detail(@PathVariable Long id){
        SysDept sysDept = sysDeptService.findById(id);
        SysDeptVo sysDeptVo = SysDeptConvert.INSTANCE.toVo(sysDept);
        return Result.success(sysDeptVo);
    }

    @PostMapping("update")
    @Operation(summary = "修改部门")
    @PreAuthorize("@lxp.hasPermission('sys:dept:update')")
    public Result update(@Valid @RequestBody SysDeptUpdateDto dto){
        sysDeptService.update(SysDeptConvert.INSTANCE.toEntity(dto));
        return Result.success();
    }

    @PostMapping("delete/{id}")
    @Operation(summary = "删除部门")
    @PreAuthorize("@lxp.hasPermission('sys:dept:delete')")
    public Result delete(@PathVariable Long id){
        sysDeptService.delete(id);
        return Result.success();
    }

}